﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using RestaurantBooker.Resources;

namespace RestaurantBooker.View
{
    public partial class MessageDlg : ChildWindowContainer
    {

        public static void ShowError(string Message)
        {
            ShowError(Message, null, null);
        }

        public static void ShowError(string Message, string Title)
        {
            ShowError(Message, Title, null);
        }

        //public bool? DialogResult
        //{ get; set; }

        private static MessageDlg _currentErrorMessageDlg = null;
        private static object _lockErrorDlgObject = new Object();

        public static void ShowError(string Message, string Title, ConfirmationResultDelegate ConfirmationResultProc)
        {

            if (Deployment.Current.Dispatcher.CheckAccess())
            {
                InternalShowError(Message, Title, ConfirmationResultProc);
            }
            else
                Deployment.Current.Dispatcher.BeginInvoke(
                    delegate
                    {
                        InternalShowError(Message, Title, ConfirmationResultProc);
                    });
        }

        private static void InternalShowError(string Message, string Title, ConfirmationResultDelegate ConfirmationResultProc)
        {
            lock (_lockErrorDlgObject)
            {
                try
                {
                    // если диалог ошибки не открыт - создадим его
                    if (_currentErrorMessageDlg == null)
                    {
                        var dlg = new MessageDlg(MessageDialogs.Error, ConfirmationResultProc);
                        dlg.imgImage.Source = StaticResources.WarningIcon32x32;
                        if (Title != null)
                            dlg.Title = Title;
                        else
                            dlg.Title = "Error";
                        dlg.txtMessageText.Text = Message;
                        dlg.CancelButton.Visibility = Visibility.Collapsed;
                        _currentErrorMessageDlg = dlg;
                        dlg.Show();
                    }
                    else
                    // если диалог ошибки уже открыт - используем его
                    {
                        lock (_lockErrorDlgObject)
                        {
                            _currentErrorMessageDlg.txtMessageText.Text +=
                                Environment.NewLine + Environment.NewLine +
                                "****************" + Environment.NewLine +
                                Message;
                        }
                    }
                }
                catch
                {
                    //(App.Current as App).RaiseAppException(Message);
                    Deployment.Current.Dispatcher.BeginInvoke(() =>
                        {
                            MessageBox.Show("Message", "Error", MessageBoxButton.OK);
                        });
                }
            }
        }

        public delegate void ConfirmationResultDelegate(bool? ConfirmResult);

        internal static void ShowInformation(string Message)
        {
            ShowInformation(Message, null);
        }

        internal static void ShowInformation(string Message, string Title)
        {
            var dlg = new MessageDlg(MessageDialogs.Information, null);
            if (Title != null)
                dlg.Title = Title;
            else
                dlg.Title = "Information";
            dlg.imgImage.Source = StaticResources.InformationIcon32x32;
            dlg.txtMessageText.Text = Message;
            dlg.OKButton.Content = "Ok";
            dlg.CancelButton.Visibility = Visibility.Collapsed;
            dlg.Show();
        }

        public static void ShowConfirmation(string Message, ConfirmationResultDelegate ConfirmationResultProc)
        {
            ShowConfirmation(Message, null, ConfirmationResultProc);
        }

        public static void ShowConfirmation(string Message, string Title, ConfirmationResultDelegate ConfirmationResultProc)
        {
            try
            {
                var dlg = new MessageDlg(MessageDialogs.Confirmation, ConfirmationResultProc);
                if (Title != null)
                    dlg.Title = Title;
                else
                    dlg.Title = "Confirmation";
                dlg.imgImage.Source = StaticResources.ConfirmationIcon32x32;
                dlg.txtMessageText.Text = Message;
                dlg.OKButton.Content = "Yes";
                dlg.CancelButton.Content = "No";
                dlg.Show();
            }
            catch
            {
                throw;
            }
        }


        private ConfirmationResultDelegate _confirmationResultProc = null;
        private MessageDialogs _messageDialog = MessageDialogs.Error;

        public MessageDlg(MessageDialogs messageDialog, ConfirmationResultDelegate ConfirmationResultProc)
        {
            InitializeComponent();

            _messageDialog = messageDialog;
            //if (ConfirmationResultProc != null)
                _confirmationResultProc = ConfirmationResultProc;
        }

        public MessageDlg(MessageDialogs messageDialog)
            : this(messageDialog, null)
        {
        }

        protected override void OnClosed(EventArgs e)
        {
            if (this._messageDialog == MessageDialogs.Error)
            {
                lock (_lockErrorDlgObject)
                {
                    _currentErrorMessageDlg = null;
                }
            }
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            CloseWindow(true);
        }

        private void CallFinishDelegate()
        {
            if (_confirmationResultProc != null)
                _confirmationResultProc(DialogResult);
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            CloseWindow(false);
        }

        private void CloseWindow(bool dialogResult)
        {
            this.DialogResult = dialogResult;
            //this.Close();

            CallFinishDelegate();
        }

        private void buttonCopy_Click(object sender, RoutedEventArgs e)
        {
            Clipboard.SetText(txtMessageText.Text);
        }

        private void ChildWindow_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Escape)
                CloseWindow(false); 
        }
    }

    public enum MessageDialogs
    {
        Error,
        Confirmation,
        Information
    }
}

